Skip to content

Conversation

mu001999
Copy link
Contributor

@mu001999 mu001999 commented Sep 24, 2025

Fixes #146967

we have had UnnamedCrateRootImport for things like use crate::self, reusing it for $crate seems good enough

Emit error CrateImported when we meet use $crate::{self}

r? petrochenkov

@rustbot
Copy link
Collaborator

rustbot commented Sep 24, 2025

Failed to set assignee to [petrochenkov]: invalid assignee

Note: Only org members with at least the repository "read" role, users with write permissions, or people who have commented on the PR may be assigned.

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Sep 24, 2025
resolve_unnamed_crate_root_import =
crate root imports need to be explicitly named: `use crate as name;`
{$dollar ->
[true] crate root imports need to be explicitly named: `use $crate as name;`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is a valid suggestion. Per this, use $crate as x; is not allowed.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Though I suppose it is a separate question if that restriction makes sense if there is an as. Is there a reason to also prevent that?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes! Updated, now will also emit $crate may not be imported for use $crate::{self} things

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The whole pre-existing logic here is garbage (#119776 (comment)), and this PR adds more.
The only restriction that we really need here is for imports to always end up with a name (*) - #35612 (comment).

use super; // bad
use super as name; // good
use super::{self}; // bad
use super::{self as name}; // good
use self; // bad
use self as name; // good
use crate; // bad
use crate as name; // good
use crate::{self}; // bad
use crate::{self as name}; // good
use $crate; // bad
use $crate as name; // good
use $crate::{self}; // bad
use $crate::{self as name}; // good

// etc

Many of these examples do not work correctly atm for various reasons.

(*) The name can even be underscore _, but it will just always be reported as unused in this case.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More than a partial fixup, I'd rather want to see an exhaustive test listing the possible cases of imports using only path segment keywords (self, super, crate, $crate) in the input path with FIXMEs as a declaration of intent.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mu001999
Could you add such a test?
Adding some partial fixup on top of that would also be fine, but ideally this stuff just needs to be rewritten.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for the case use $crate as name; // good in the above examples, @ehuss said in rust-lang/reference#2010 (comment):

but for some reason use $crate as foo doesn't work either.


And I'd like to rewrite the whole logic

ehuss added a commit to ehuss/reference that referenced this pull request Sep 24, 2025
The previous wording for this restriction was pretty confusing to me. I
don't remember what I was thinking when I wrote it, and I can't find any
historical explanation either. `use` paths can use `$crate` as long as
they have more than one segment (`use $crate::foo` is obviously OK).

I have rewritten this to make it clear it is specifically about `use
$crate`. One could say that restriction is already covered by the
previous point that says `use crate;` requires an `as`, but for some
reason `use $crate as foo` doesn't work either. So I have left this as a
separate rule for now.

cc rust-lang/rust#146972 (comment)
for context.
@mu001999 mu001999 force-pushed the fix/use-dollar-crate branch from ce2578f to 49c425d Compare September 25, 2025 01:54
@mu001999 mu001999 changed the title Emit error UnnamedCrateRootImport also for DollarCrate Disable use $crate::{self} like use $crate Sep 25, 2025
@petrochenkov petrochenkov added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Sep 26, 2025
@mu001999 mu001999 marked this pull request as draft October 8, 2025 06:43
@mu001999 mu001999 force-pushed the fix/use-dollar-crate branch from 49c425d to d0d3a9d Compare October 9, 2025 03:40
@rust-log-analyzer
Copy link
Collaborator

The job aarch64-gnu-llvm-20-1 failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)
To only update this specific test, also pass `--test-args rfcs/rfc-2126-extern-absolute-paths/single-segment.rs`

error: 1 errors occurred comparing output.
status: exit status: 1
command: env -u RUSTC_LOG_COLOR RUSTC_ICE="0" RUST_BACKTRACE="short" "/checkout/obj/build/aarch64-unknown-linux-gnu/stage2/bin/rustc" "/checkout/tests/ui/rfcs/rfc-2126-extern-absolute-paths/single-segment.rs" "-Zthreads=1" "-Zsimulate-remapped-rust-src-base=/rustc/FAKE_PREFIX" "-Ztranslate-remapped-path-to-local-path=no" "-Z" "ignore-directory-in-diagnostics-source-blocks=/cargo" "-Z" "ignore-directory-in-diagnostics-source-blocks=/checkout/vendor" "--sysroot" "/checkout/obj/build/aarch64-unknown-linux-gnu/stage2" "--target=aarch64-unknown-linux-gnu" "--check-cfg" "cfg(test,FALSE)" "--error-format" "json" "--json" "future-incompat" "-Ccodegen-units=1" "-Zui-testing" "-Zdeduplicate-diagnostics=no" "-Zwrite-long-types-to-disk=no" "-Cstrip=debuginfo" "--emit" "metadata" "-C" "prefer-dynamic" "--out-dir" "/checkout/obj/build/aarch64-unknown-linux-gnu/test/ui/rfcs/rfc-2126-extern-absolute-paths/single-segment" "-A" "unused" "-A" "internal_features" "-A" "unused_parens" "-A" "unused_braces" "-Crpath" "-Cdebuginfo=0" "-Lnative=/checkout/obj/build/aarch64-unknown-linux-gnu/native/rust-test-helpers" "-L" "/checkout/obj/build/aarch64-unknown-linux-gnu/test/ui/rfcs/rfc-2126-extern-absolute-paths/single-segment/auxiliary" "--edition=2018" "--extern" "xcrate"
stdout: none
--- stderr -------------------------------
error: imports need to be explicitly named: `use crate as name;`
##[error]  --> /checkout/tests/ui/rfcs/rfc-2126-extern-absolute-paths/single-segment.rs:5:5
   |
LL | use crate; //~ ERROR crate root imports need to be explicitly named: `use crate as name;`
   |     ^^^^^

error: cannot glob-import all possible crates
##[error]  --> /checkout/tests/ui/rfcs/rfc-2126-extern-absolute-paths/single-segment.rs:6:5
   |
LL | use *; //~ ERROR cannot glob-import all possible crates
   |     ^

error[E0423]: expected value, found crate `xcrate`
##[error]  --> /checkout/tests/ui/rfcs/rfc-2126-extern-absolute-paths/single-segment.rs:9:13
   |
LL |     let s = ::xcrate; //~ ERROR expected value, found crate `xcrate`
   |             ^^^^^^^^ not a value

error: aborting due to 3 previous errors

For more information about this error, try `rustc --explain E0423`.
---
To only update this specific test, also pass `--test-args use/use-keyword.rs`

error: 1 errors occurred comparing output.
status: exit status: 1
command: env -u RUSTC_LOG_COLOR RUSTC_ICE="0" RUST_BACKTRACE="short" "/checkout/obj/build/aarch64-unknown-linux-gnu/stage2/bin/rustc" "/checkout/tests/ui/use/use-keyword.rs" "-Zthreads=1" "-Zsimulate-remapped-rust-src-base=/rustc/FAKE_PREFIX" "-Ztranslate-remapped-path-to-local-path=no" "-Z" "ignore-directory-in-diagnostics-source-blocks=/cargo" "-Z" "ignore-directory-in-diagnostics-source-blocks=/checkout/vendor" "--sysroot" "/checkout/obj/build/aarch64-unknown-linux-gnu/stage2" "--target=aarch64-unknown-linux-gnu" "--check-cfg" "cfg(test,FALSE)" "--error-format" "json" "--json" "future-incompat" "-Ccodegen-units=1" "-Zui-testing" "-Zdeduplicate-diagnostics=no" "-Zwrite-long-types-to-disk=no" "-Cstrip=debuginfo" "--emit" "metadata" "-C" "prefer-dynamic" "--out-dir" "/checkout/obj/build/aarch64-unknown-linux-gnu/test/ui/use/use-keyword" "-A" "unused" "-A" "internal_features" "-A" "unused_parens" "-A" "unused_braces" "-Crpath" "-Cdebuginfo=0" "-Lnative=/checkout/obj/build/aarch64-unknown-linux-gnu/native/rust-test-helpers"
stdout: none
--- stderr -------------------------------
error[E0429]: `self` imports are only allowed within a { } list
##[error]  --> /checkout/tests/ui/use/use-keyword.rs:6:13
   |
LL |         use self as A;
   |             ^^^^

---
error: ui test did not emit an error
note: by default, ui tests are expected not to compile.
hint: use check-pass, build-pass, or run-pass directive to change this behavior.
status: exit status: 0
command: env -u RUSTC_LOG_COLOR RUSTC_ICE="0" RUST_BACKTRACE="short" "/checkout/obj/build/aarch64-unknown-linux-gnu/stage2/bin/rustc" "/checkout/tests/ui/use/use-mod/use-mod-2.rs" "-Zthreads=1" "-Zsimulate-remapped-rust-src-base=/rustc/FAKE_PREFIX" "-Ztranslate-remapped-path-to-local-path=no" "-Z" "ignore-directory-in-diagnostics-source-blocks=/cargo" "-Z" "ignore-directory-in-diagnostics-source-blocks=/checkout/vendor" "--sysroot" "/checkout/obj/build/aarch64-unknown-linux-gnu/stage2" "--target=aarch64-unknown-linux-gnu" "--check-cfg" "cfg(test,FALSE)" "--error-format" "json" "--json" "future-incompat" "-Ccodegen-units=1" "-Zui-testing" "-Zdeduplicate-diagnostics=no" "-Zwrite-long-types-to-disk=no" "-Cstrip=debuginfo" "--emit" "metadata" "-C" "prefer-dynamic" "--out-dir" "/checkout/obj/build/aarch64-unknown-linux-gnu/test/ui/use/use-mod/use-mod-2" "-A" "unused" "-A" "internal_features" "-A" "unused_parens" "-A" "unused_braces" "-Crpath" "-Cdebuginfo=0" "-Lnative=/checkout/obj/build/aarch64-unknown-linux-gnu/native/rust-test-helpers"
stdout: none
stderr: none

---- [ui] tests/ui/use/use-mod/use-mod-2.rs stdout end ----
---- [ui] tests/ui/use/use-path-segment-kw.rs stdout ----

error: ui test did not emit an error
note: by default, ui tests are expected not to compile.
hint: use check-pass, build-pass, or run-pass directive to change this behavior.
status: exit status: 0
command: env -u RUSTC_LOG_COLOR RUSTC_ICE="0" RUST_BACKTRACE="short" "/checkout/obj/build/aarch64-unknown-linux-gnu/stage2/bin/rustc" "/checkout/tests/ui/use/use-path-segment-kw.rs" "-Zthreads=1" "-Zsimulate-remapped-rust-src-base=/rustc/FAKE_PREFIX" "-Ztranslate-remapped-path-to-local-path=no" "-Z" "ignore-directory-in-diagnostics-source-blocks=/cargo" "-Z" "ignore-directory-in-diagnostics-source-blocks=/checkout/vendor" "--sysroot" "/checkout/obj/build/aarch64-unknown-linux-gnu/stage2" "--target=aarch64-unknown-linux-gnu" "--check-cfg" "cfg(test,FALSE)" "--error-format" "json" "--json" "future-incompat" "-Ccodegen-units=1" "-Zui-testing" "-Zdeduplicate-diagnostics=no" "-Zwrite-long-types-to-disk=no" "-Cstrip=debuginfo" "--emit" "metadata" "-C" "prefer-dynamic" "--out-dir" "/checkout/obj/build/aarch64-unknown-linux-gnu/test/ui/use/use-path-segment-kw" "-A" "unused" "-A" "internal_features" "-A" "unused_parens" "-A" "unused_braces" "-Crpath" "-Cdebuginfo=0" "-Lnative=/checkout/obj/build/aarch64-unknown-linux-gnu/native/rust-test-helpers"
stdout: none
stderr: none

---- [ui] tests/ui/use/use-path-segment-kw.rs stdout end ----

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

use $crate::{self}; should not be accepted
5 participants